from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
import pickle
import tqdm

# 加载数字数据集
digits = datasets.load_digits()

# 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=42)

# 初始化变量以存储最佳准确率，相应的k值和最佳knn模型
best_accuracy = 0
best_k = -1
best_knn = None

# 初始化一个列表来保存所有的准确率和对应的k值
acc_list=[]

# 尝试从1到40的k值，对于每个k值，训练knn模型，保存最佳准确率，k值和knn模型
for k in tqdm.tqdm(range(1, 41)):
    knn = KNeighborsClassifier(n_neighbors=k)
    knn.fit(X_train, y_train)
    y_pred = knn.predict(X_test)
    accuracy = accuracy_score(y_test, y_pred)
    acc_list.append(accuracy)
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn = knn

# 将最佳KNN模型保存到二进制文件
with open('best_knn.pkl', 'wb') as f:
    pickle.dump(best_knn, f)
    print("Best Accuracy: ", best_accuracy)
print("Best K: ", best_k)

# 打印最佳准确率和相应的k值
text='k='+str(best_k)+','+'accuracy='+str(best_accuracy)
plt.figure()
x=range(1,41)
y1=acc_list
plt.plot(x,y1)
plt.text(best_k, best_accuracy, text,fontsize=12)
plt.xlabel('k')
plt.ylabel('Accuracy')
plt.title('Accuracy for different k values')
plt.axvline(x=best_k, color='r', linestyle='--', label='Best k')
plt.grid(True)
plt.savefig('accuracy_plot.pdf')